#include "basic.h"
#include <stdio.h>
#include <math.h>
#include "basic.hpp"


__STATIC_INLINE uint32_t GXT_SYSTICK_IsActiveCounterFlag(void)
{
    return ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == (SysTick_CTRL_COUNTFLAG_Msk));
}
uint32_t get_ms()
{
    return HAL_GetTick();
}
uint32_t get_us()
{
    /* Ensure COUNTFLAG is reset by reading SysTick control and status register */
    GXT_SYSTICK_IsActiveCounterFlag();
    uint32_t m = HAL_GetTick();
    const uint32_t tms = SysTick->LOAD + 1;
    uint32_t u = tms - SysTick->VAL;
    if (GXT_SYSTICK_IsActiveCounterFlag())
    {
        m = HAL_GetTick();
        u = tms - SysTick->VAL;
    }
    return (m * 1000 + (u * 1000) / tms);
}

void delay(uint32_t ms)
{
    uint32_t start = get_ms();
    while (get_ms() - start < ms)
        ;
}
void delay_us(uint32_t us)
{
    uint32_t start = get_us();
    while (get_us() - start < us)
        ;
}

int _10_pow(uint8_t n)
{
    int ret = 1;
    for (uint8_t i = 0; i < n; i++)
    {
        ret *= 10;
    }
    return ret;
}

void print_float(float value, uint8_t precision)
{
    if (precision > 6)
        precision = 6; /**/

    if (value < 0)
    {
        serial_send_char('-');
        value = -value;
    }
    if (fabsf(value) < 1e-6)
    {
        serial_send_char('0');
        return;
    }

    // 提取整数部分
    int32_t integer_part = (int32_t)value;

    // 提取小数部分
    float fractional_part = value - integer_part;
    int32_t fractional_int = 0;

    // 根据精度计算小数部分
    if (precision > 0)
    {
        fractional_int = (int32_t)(fractional_part * _10_pow(precision) + 0.5); // 四舍五入
    }

    if (precision == 0)
    {
        print_int(integer_part);
    }
    else
    {
        print_int(integer_part);
        serial_send_char('.');

        while (fractional_int / _10_pow((precision--) - 1) == 0 && precision > 0)
            serial_send_char('0');

        print_int(fractional_int);
    }
}
void print_int(int64_t i)
{
    if (i < 0)
    {
        i = -i;
        serial_send_char('-');
    }

    char buffer[32]; /*0xffff ffff ffff ffff=9,223,372,036,854,775,807*/
    uint8_t precision_ = 0;

    do
    {
        buffer[precision_++] = i % 10 + '0';
        i /= 10;
    } while (i);

    for (int8_t i = precision_ - 1; i >= 0; i--)
    {
        serial_send_char(buffer[i]);
    }
    // serial_send_char('\0');
}
void print_uint(uint64_t i)
{
    if (i == 0)
    {
        serial_send_char('0');
        return;
    }

    char buffer[32]; /*0xffff ffff ffff ffff=18,446,744,073,709,551,615*/
    uint8_t precision_ = 0;

    do
    {
        buffer[precision_++] = i % 10 + '0';
        i /= 10;
    } while (i);

    for (int8_t i = precision_ - 1; i >= 0; i--)
    {
        serial_send_char(buffer[i]);
    }
    // serial_send_char('\0');
}



float fabsf(float f)
{
    return (f < 0) ? -f : f;
}

void LOGI(const char *tag, const char *str)
{
    serial_send_char('[');
    serial_send(tag);
    serial_send("] :");
    serial_send(str);
    serial_send_char('\n');
}
namespace selfHandler
{
    MyPrint &cout = MyPrint::instance();

    MyPrint &endl(MyPrint &mp)
    {
        mp << "\n"; // 发送换行
        return mp;
    }

    // 构造函数
    MyPrint::MyPrint() : float_precision(3) {}

    // 获取单例实例
    MyPrint &MyPrint::instance()
    {
        static MyPrint instance;
        return instance;
    }

    // 设置浮点数精度
    MyPrint &MyPrint::set_precision(uint8_t prec)
    {
        float_precision = prec;
        return *this;
    }

    // 处理字符串
    MyPrint &MyPrint::operator<<(const char *str)
    {
        serial_send(str);
        return *this;
    }
    MyPrint &MyPrint::operator<<(const char ch)
    {
        serial_send_char(ch);
        return *this;
    }

    // 处理整数
    MyPrint &MyPrint::operator<<(int64_t value)
    {
        print_int(value);
        return *this;
    }
    MyPrint &MyPrint::operator<<(int32_t value)
    {
        print_int((int64_t)value);
        return *this;
    }
    MyPrint &MyPrint::operator<<(int value)
    {
        print_int((int64_t)value);
        return *this;
    }
    MyPrint &MyPrint::operator<<(int8_t value)
    {
        print_int((int8_t)value);
        return *this;
    } 
    MyPrint &MyPrint::operator<<(uint64_t value)
    {
        print_uint(value);
        return *this;
    }
    MyPrint &MyPrint::operator<<(uint32_t value)
    {
        print_uint((uint64_t)value);
        return *this;
    }
    MyPrint &MyPrint::operator<<(uint16_t value)
    {
        print_uint((uint64_t)value);
        return *this;
    }
    MyPrint &MyPrint::operator<<(uint8_t value)
    {
        print_uint((uint64_t)value);
        return *this;
    }
    MyPrint &MyPrint::operator<<(bool value)
    {
        serial_send(value ? "true" : "false");
        return *this;
    }

    // 处理浮点数
    MyPrint &MyPrint::operator<<(float value)
    {
        print_float(value, float_precision);
        return *this;
    }

    // 处理操纵符
    MyPrint &MyPrint::operator<<(MyPrint &(*manip)(MyPrint &))
    {
        return manip(*this);
    }

}